package com.wu.controller;

import com.wu.config.ConfirmConfig;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @program: RabbitMQ-Study
 * @author: 吴磊
 * @create: 2021-12-19 21:03
 * 测试确认
 */
@RestController
@Api(tags = "测试确认")
@RequestMapping("/confirm")
@Slf4j
public class ConfirmController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMessage/{message}")
    public void sendMessage(@PathVariable("message") String message) {
        //情况一：交换机收到消息   消费者也可以收到消息
        String id1 = UUID.randomUUID().toString().replaceAll("-", "");
        CorrelationData correlationData1 = new CorrelationData(id1);
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY, message, correlationData1);
        log.info("发送消息内容为：{}，routingKey为：{}，id为{}", message, ConfirmConfig.CONFIRM_ROUTING_KEY, id1);

        //情况二：交换机收到消息    消费者不能收到消息（routingkey变了：CONFIRM_ROUTING_KEY + "1"）
        String id2 = UUID.randomUUID().toString().replaceAll("-", "");
        CorrelationData correlationData2 = new CorrelationData(id2);
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY + "1", message, correlationData2);
        log.info("发送消息内容为：{}，routingKey为：{}，id为{}", message, ConfirmConfig.CONFIRM_ROUTING_KEY + "1", id2);

        //情况三：交换机收不到消息(用改交换机名字的方式模仿交换机宕机的情况：CONFIRM_EXCHANGE_NAME + "1")
        //String id3 = UUID.randomUUID().toString().replaceAll("-", "");
        //CorrelationData correlationData3 = new CorrelationData(id3);
        //rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME + "1", ConfirmConfig.CONFIRM_ROUTING_KEY, message, correlationData3);
        //log.info("发送消息内容为：{}，routingKey为：{}，id为{}", message, ConfirmConfig.CONFIRM_ROUTING_KEY, id3);
    }
}
